package com.wh.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wh.constant.MessageConstant;
import com.wh.entity.PageResult;
import com.wh.entity.QueryPageBean;
import com.wh.entity.Result;
import com.wh.pojo.Dept;
import com.wh.service.DeptService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Reference
    private DeptService deptService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = deptService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //添加部门
    @RequestMapping("/add")
    public Result add(@RequestBody Dept dept){
        try {
            deptService.add(dept);
            return new Result(true, MessageConstant.ADD_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_DEPT_FAIL);
        }
    }

    //修改部门
        @RequestMapping("/edit")
        public Result edit(@RequestBody Dept dept){
            try {
                deptService.edit(dept);
                return new Result(true,MessageConstant.EDIT_DEPT_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.EDIT_DEPT_FAIL);
            }
    }

    //通过id找部门
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Dept dept = deptService.findById(id);
            return new Result(true,MessageConstant.QUERY_DEPT_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

    //删除部门
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            deptService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_DEPT_FAIL);
        }
    }
}
